package JunitTests;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import uk.ac.glasgow.demosocs.Candidate;
import uk.ac.glasgow.demosocs.users.CandidateImpl;
import uk.ac.glasgow.demosocs.users.Voter;

public class VoterTest {
	private Voter voter1;
	private Voter voter2;
	private CandidateImpl candidate1;
	private CandidateImpl candidate2;
	
	
	@Before
	public void setUp() throws Exception {
		voter1 = new Voter("mysurname_v1","myforename_v1","guid1","123");
		voter2 = new Voter("mysurname_v2","myforename_v2","guid2","123");
		candidate1 = new CandidateImpl("mysurname_c1","myforename_c1","guid3","123");
		candidate2 = new CandidateImpl("mysurname_c2","myforename_c2","guid4","123");
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	/**
	 * Check if a voter can cast a vote
	 */
	public void testCastVote1() {
		//Voter1 casts a vote to candidate1
		voter1.castVote(candidate1);
		//See if the votes of the candidate1 are 1
		assertEquals(candidate1.getVotes(), 1);	
	}
	
	@Test
	/**
	 * Check if a voter can vote the same candidate twice
	 */
	public void testCastVote2() {
		//Voter1 casts a vote to candidate1
		voter1.castVote(candidate1);
		//Voter1 casts again a vote to candidate1
		voter1.castVote(candidate1);
		//Check if the candidate received two votes from the same voter
		assertEquals(candidate1.getVotes(), 1);	
	}
	
	@Test
	/**
	 * Check if two voters can cast a vote to the same candidate
	 */
	public void testCastVote3() {
		//Voter1 casts a vote to candidate1
		voter1.castVote(candidate1);
		//Voter2 casts a vote to candidate1
		voter2.castVote(candidate1);
		//See if the votes of the candidate1 are 2
		assertEquals(candidate1.getVotes(), 2);	
	}
	
	@Test
	/**
	 * Check if one voter can cast a vote to two candidates
	 */
	public void testCastVote4() {
		//Voter1 casts a vote to candidate1
		voter1.castVote(candidate1);
		//Voter1 casts a vote to candidate2
		voter1.castVote(candidate2);
		//Check if the the vote is allocated to the second candidate
		assertEquals(candidate1.getVotes(), 0);
		assertEquals(candidate2.getVotes(), 1);
	}

	@Test
	/**
	 * Check if the votes are reset
	 */
	public void testResetVote() {
		//Voter1 casts a vote to candidate1
		voter1.castVote(candidate1);
		//Reset vote of voter1
		voter1.resetVote();
		//Check if candidate1 has assigned a vote after the reset
		assertEquals(candidate1.getVotes(),0);
	}

//	@Test
//	public void testBeNominatedByOther() {
//		fail("Not yet implemented");
//	}

}
